﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using olierFrm.DataBase.FrameWork.Plugin;

namespace olierFrm.Web
{

    public class Receipt
    {
        private string IniScript;
        Dictionary<string, Dictionary<string, string>> diIniObject;
        //public Receipt(string iniScript)
        //{
        //    this.IniScript = iniScript;
        //}

        public Receipt(string module)
        {
            ReceiptsConfig rc = new ReceiptsConfig(module);
            if (rc.IniScript != null && rc.IniScript.Length != 0)
            {
                this.IniScript = rc.IniScript;
            }
        }

        public Module LoadModule()
        {
            Module module = new Module();
            #region SmartLookup
            var sls = GetSection("SmartLookup");
            module.DataLinks = new Dictionary<int, List<DataLink>>();



            if (sls != null && sls.Count != 0)
            {
                int i = sls.ContainsKey("Main") ? 0 : 1;
                foreach (var s in sls)
                {
                    if (s.Value != null && s.Value.Length != 0)
                    {
                        var svs = s.Value.Split(new char[] { ';' }).ToList();
                        svs.Remove("");
                        List<DataLink> dls = new List<DataLink>();
                        foreach (var d in svs)
                        {
                            DataWrapperLookup dwl = new DataWrapperLookup(d);
                            foreach (var dw in dwl.WrapperLookups)
                            {
                                string query;
                                DataLink dl = new DataLink();
                                dl.KeyField = dw.KeyField.Split(';').ToList<string>();
                                dl.KeyField.Remove("");
                                dl.LookupField = dw.LookupField;
                                dl.Type = dw.GetQuery(out query);
                                dl.Query = query;
                                dl.WrapperName = dw.WrapperName;
                                dls.Add(dl);
                            }
                        }
                        module.DataLinks.Add(i, dls);
                    }
                    i++;
                }
            }
            #endregion

            module.Business = GetSection("Business");

            #region DataObject
            var dataObject = GetSection("DataObject");
            if (dataObject != null)
            {
                module.DataObject = dataObject["ObjectName"];
                dataObject.Remove("ObjectName");
                module.DataObjectLabel = new List<string>();
                foreach (var d in dataObject)
                {
                    module.DataObjectLabel.Add(d.Value);
                }
            }
            #endregion

            #region StdQuery
            var stdQueyr = GetSection("StdQuery");
            if (stdQueyr != null)
                module.SmartQuery = stdQueyr["QueryName"];
            #endregion

            #region Fields Display
            var fieldsDisplay = GetSection("Fields Display");
            if (fieldsDisplay != null)
            {
                module.SumFields = SectionToList(fieldsDisplay, "SumFields");
                module.ReadOnly = SectionToList(fieldsDisplay, "ReadOnly");
                module.Feedback = SectionToList(fieldsDisplay, "Feedback");
                module.Display = SectionToList(fieldsDisplay, "Display");
                module.DicContext = SectionToDictionary(fieldsDisplay, "DicContext");
            }
            #endregion
            return module;
        }

        public Scattered LoadScattered()
        {

            Scattered scattered = new Scattered();

            #region StdQuery

            var stdquery = GetPrivateProfileString("StdQuery", "SqlID");
            if (stdquery != null && stdquery.Length != 0)
            {
                var ss = stdquery.Split(new char[] { ';' }).ToList();
                ss.Remove("");
                scattered.SmartQuery = ss;
            }
            else
            {
                scattered.SmartQuery = new List<string>();
            }

            #endregion


            #region DataObject
            var dataObject = GetPrivateProfileString("DataObject", "SqlID");
            if (dataObject != null && dataObject.Length != 0)
            {
                var ss = dataObject.Split(new char[] { ';' }).ToList();
                ss.Remove("");
                scattered.DataObject = ss;
            }
            else
            {
                scattered.DataObject = new List<string>();
            }
            #endregion

            return scattered;
        }

        private Dictionary<int, Dictionary<string, string>> SectionToDictionary(Dictionary<string, string> section, string key)
        {
            Dictionary<int, Dictionary<string, string>> dis = new Dictionary<int, Dictionary<string, string>>();

            var dcs = section.Where(f => f.Key.IndexOf(key) != -1).ToList();
            if (dcs.Count != 0)
            {

                for (int i = 0; i < dcs.Count; i++)
                {
                    if (dcs[i].Value != null && dcs[i].Value.Length != 0)
                    {
                        var sps = dcs[i].Value.Split(new char[] { ';' }).ToList();
                        var sss = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                        foreach (var s in sps)
                        {
                            var ss = s.Split('=');
                            sss.Add(ss[0], ss[1]);
                        }
                        dis.Add(i, sss);
                    }
                }
                return dis;
            }

            return null;
        }

        private Dictionary<int, List<string>> SectionToList(Dictionary<string, string> section, string key)
        {
            Dictionary<int, List<string>> dis = new Dictionary<int, List<string>>();
            var secs = section.Where(s => s.Key.IndexOf(key) != -1).ToList();
            if (secs.Count != 0)
            {

                for (int i = 0; i < secs.Count; i++)
                {
                    if (secs[i].Value != null && secs[i].Value.Length != 0)
                    {
                        var sps = secs[i].Value.Split(new char[] { ';' }).ToList();
                        sps.Remove("");
                        dis.Add(i, sps);
                    }
                }
                return dis;
            }
            return null;
        }

        private Dictionary<string, string> GetSection(string section)
        {
            if (this.diIniObject == null)
            {
                this.diIniObject = SmartINI.LoadIni(this.IniScript);
            }
            Dictionary<string, string> result;
            if (this.diIniObject.ContainsKey(section))
            {
                result = this.diIniObject[section];
            }
            else
            {
                result = null;
            }
            return result;
        }

        private string GetPrivateProfileString(string section, string key)
        {
            Dictionary<string, string> sections = this.GetSection(section);
            string result;
            if (sections == null || !sections.ContainsKey(key))
            {
                result = null;
            }
            else
            {
                result = sections[key];
            }
            return result;
        }

        public enum ReceiptType
        {
            Scattered = 0,
            Module = 1
        }

        [Serializable]
        public class Scattered
        {
            public List<string> SmartQuery { get; set; }
            public List<string> DataObject { get; set; }
        }
        [Serializable]
        public class Module
        {
            public string SmartQuery { get; set; }
            public string DataObject { get; set; }
            /// <summary>
            /// 从表1开始
            /// </summary>
            public List<string> DataObjectLabel { get; set; }
            public Dictionary<string, string> Business { get; set; }
            public Dictionary<int, List<DataLink>> DataLinks { get; set; }
            /// <summary>
            /// 从表1开始
            /// </summary>
            public Dictionary<int, List<string>> SumFields { get; set; }
            /// <summary>
            /// 从表0开始
            /// </summary>
            public Dictionary<int, List<string>> ReadOnly { get; set; }
            /// <summary>
            /// 从表0开始
            /// </summary>
            public Dictionary<int, List<string>> Display { get; set; }
            /// <summary>
            /// 从表0开始
            /// </summary>
            public Dictionary<int, List<string>> Feedback { get; set; }
            /// <summary>
            /// 从表0开始
            /// </summary>
            public Dictionary<int, Dictionary<string, string>> DicContext { get; set; }
        }

        public class DataLink
        {
            public string WrapperName { get; set; }
            public List<string> KeyField { get; set; }
            public string LookupField { get; set; }
            public string Query { get; set; }
            public int Type { get; set; }
        }
    }
}
